#include <iostream>
#include "SGL.h"

using namespace std;
using namespace SGLEngine;

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

class CSquare : public CObject2D {
	public:
		CSquare()
		{
			
		}
	void init(SGL* engine)
	{
		this->addLine(vector2di(1,1), vector2di(1,100), engine->getSGLColor()->DARK_GREEN);
		this->addLine(vector2di(1,100), vector2di(100,100), engine->getSGLColor()->DARK_GREEN);
		this->addLine(vector2di(100,100), vector2di(100,1), engine->getSGLColor()->DARK_GREEN);
		this->addLine(vector2di(100,1), vector2di(1,1), engine->getSGLColor()->DARK_GREEN);
	}
};

class Enemy : public CObject2D {
	void init(SGL* engine)
	{
		this->addLine(vector2di(1,1), vector2di(100,1), engine->getSGLColor()->WHITE);
		this->addLine(vector2di(100,1), vector2di(50,50), engine->getSGLColor()->WHITE);
		this->addLine(vector2di(50,50), vector2di(1,1), engine->getSGLColor()->WHITE);
	}
};

class CBullet : public CObject2D {

};

class MoveSquare : public SGLAction {
	public:
		MoveSquare()
		{

		}
		virtual void Action(const irr::SEvent& irrevent, SGL* engine)
		{
			if (irrevent.KeyInput.Key == irr::KEY_DOWN && irrevent.KeyInput.PressedDown)
				engine->get2DObject("cs1")->Move(0,8);

			if (irrevent.KeyInput.Key == irr::KEY_UP && irrevent.KeyInput.PressedDown)
				engine->get2DObject("cs1")->Move(0,-8);

			if (irrevent.KeyInput.Key == irr::KEY_LEFT && irrevent.KeyInput.PressedDown)
			{
				engine->get2DObject("cs1")->Move(-8,0);
				engine->get2DObject("bullet")->Move(-8, 0);
			}

			if (irrevent.KeyInput.Key == irr::KEY_RIGHT && irrevent.KeyInput.PressedDown)
			{
				engine->get2DObject("cs1")->Move(8,0);
				engine->get2DObject("bullet")->Move(8, 0);
			}
		}
};

class Esckey : public SGLAction {
	virtual void Action(const irr::SEvent& irrevent, SGL* engine)
	{
		if (irrevent.KeyInput.Key == irr::KEY_ESCAPE && irrevent.KeyInput.PressedDown)
			engine->End();
	}
};

class FireAction : public SGLAction {
	virtual void Action(const irr::SEvent& irrevent, SGL* engine)
	{
		if (irrevent.KeyInput.Key == irr::KEY_SPACE && irrevent.KeyInput.PressedDown)
			engine->get2DObject("bullet")->setYVelocity(-1);
	}
};

class ReloadAction : public SGLAction {
	virtual void Action(const irr::SEvent& irrevent, SGL* engine)
	{
		if (irrevent.KeyInput.Key == irr::KEY_KEY_R)
		{
			engine->get2DObject("e1")->setDisplay(true);
			engine->get2DObject("e2")->setDisplay(true);
		}
	}
};

start()
{
	SGLSettings set;
	set.DriverType = irr::video::EDT_OPENGL;
	SGL engine(set);
	CBullet * bullet;
	bullet = new CBullet();
	bullet->addLine(vector2di(1,1), vector2di(1,100), engine.getSGLColor()->DARK_GREEN);
	bullet->Move(300, 350);
	engine.AddKeyEvent(new MoveSquare());
	engine.AddKeyEvent(new Esckey());
	engine.AddKeyEvent(new FireAction());
	engine.AddKeyEvent(new ReloadAction());
	engine.AddObject(new CSquare(), "cs1");
	engine.AddObject(bullet, "bullet");
	engine.AddObject(new Enemy(), "e1");
	engine.AddObject(new Enemy(), "e2");
	engine.get2DObject("e1")->Move(200, 50);
	engine.get2DObject("e2")->Move(400, 50);
	engine.get2DObject("cs1")->Move(250, 500);
	irr::video::SColor back = engine.getSGLColor()->BLACK;
	SGLSceneSettings sceneset;
	sceneset.color = back;
	while(engine.DeviceIsRunning())
	{
		if (bullet->getY() < 0)
		{
			irr::s32 bx, by;
			bx = engine.get2DObject("cs1")->getX();
			by = engine.get2DObject("cs1")->getY();
			bullet->reset();
			bullet->setYVelocity(0);
			bullet->Move(bx+50, by-150);
		}
		if (bullet->collidesWith(engine.get2DObject("e1")))
		{
			irr::s32 bx, by;
			bx = engine.get2DObject("cs1")->getX();
			by = engine.get2DObject("cs1")->getY();
			bullet->reset();
			bullet->setYVelocity(0);
			bullet->Move(bx+50, by-150);
			engine.get2DObject("e1")->setDisplay(false);
		}
		if (bullet->collidesWith(engine.get2DObject("e2")))
		{
			irr::s32 bx, by;
			bx = engine.get2DObject("cs1")->getX();
			by = engine.get2DObject("cs1")->getY();
			bullet->reset();
			bullet->setYVelocity(0);
			bullet->Move(bx+50, by-150);
			engine.get2DObject("e2")->setDisplay(false);
		}
		engine.BeginScene(sceneset);
		engine.DrawAll();

	}
	return 0;
}